import { createSlice } from "@reduxjs/toolkit";

const reducer = createSlice({
  // 仓库名
  name: "reducer",
  // 仓库默认值
  initialState: {
    name: "张三",
    age: 18,
    data: [],
    childrenData: [],
  },
  // 修改仓库的方法
  reducers: {
    /**
     *
     * @param {*} state 仓库数据
     * @param {*} value payload 参数 ,type action类型
     */
    set_name(state, value) {
      state.name = value.payload;
    },
    set_data(state, { payload }) {
      state.data = payload;
      state.childrenData = payload[0].children;
    },
    set_childrendata(state, { payload }) {
      state.childrenData = state.data[payload].children;
    },
  },
});

// 导出action
export const { set_name, set_data, set_childrendata } = reducer.actions;
// 导出子仓库
export default reducer.reducer;
